package com.hjimi.ski.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;

/**
 * Created By 孙岩
 * on 2023/7/14
 */
public class DiscoverNetIpUtil {

        /**
         * 获取用户IP地址<br/>
         * 注意：需要在androidManifest.xml中声明下面三个权限才能正常使用该方法，否则会空指针异常
         * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
         * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
         * <uses-permission android:name="android.permission.INTERNET"/>
         */
        public static String getIpAddress(Context context) {
            if (context == null) {
                return "";
            }

            ConnectivityManager conMann = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo wifiNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
             if (wifiNetworkInfo.isConnected()) {
                return getWifiAddress(context);
            }
            return "";
        }

        private static String getLocalIpAddress() {
            try {
                ArrayList<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
                for (NetworkInterface ni : nilist) {
                    ArrayList<InetAddress> ialist = Collections.list(ni.getInetAddresses());
                    for (InetAddress address : ialist) {
                        if (!address.isLoopbackAddress() && address instanceof Inet4Address) {
                            return address.getHostAddress();
                        }
                    }
                }
            } catch (SocketException e) {
                e.printStackTrace();
            }
            return "";
        }

        private static String getWifiAddress(Context context) {
            if (context == null) {
                return "";
            }
            WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int ipAddress = wifiInfo.getIpAddress();
            return intToIp(ipAddress);
        }

        private static String intToIp(int ipInt) {
            String s = (ipInt & 0xFF) + "." +
                    ((ipInt >> 8) & 0xFF) + "." +
                    ((ipInt >> 16) & 0xFF) + "." +
                    ((ipInt >> 24) & 0xFF);
            return s;
        }
    }

